package com.edu.interceptor;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyInterceptor implements HandlerInterceptor {

    /**
     *
     * @param request  请求对象
     * @param response 相应对象
     * @param handler  被拦截到的 那个Controller
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("执行了 拦截器的前置处理");
        HandlerMethod hm = (HandlerMethod) handler;
        System.out.println(hm.getBean());
        System.out.println(hm.getMethod().getName());
//        request.getRequestDispatcher("/index2.jsp").forward(request,response);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("执行了 拦截器的 后置处理");
        modelAndView.setViewName("forward:/index3.jsp");
        modelAndView.addObject("msg","被篡改的数据");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("执行了最终处理");
        if(ex != null){
            request.getRequestDispatcher("/error.jsp").forward(request,response);
        }
    }
}
